/*
 * Copyright (c) 2002-2004, xRhino, Inc.
 *
 * This library is free software; you can redistribute and/or modify
 * it under the terms of the dual license distributed with this source
 * package (a combined Trolltech QPL/GPL-compatible license)
 *
 * Under this dual license scheme, a separate license is available for use of 
 * this code with a proprietary (closed) system. For more information on the 
 * license, please see the LICENSE file distributed with this source package.
 */
#include <map>
#include <ctype.h>
#include "SugColor.h"

/**
 * Variable indicating whether the name->color map has been
 * initilized or not.
 */
static bool gColorsInitilized = false;

/**
 * A map mapping the names of colors to their RGB values.
 */
static map<string, unsigned int> gColors;

SugRealType SugColor::getRed()
{
   return mRGB[0];
}

SugRealType SugColor::getGreen()
{
   return mRGB[1];
}

SugRealType SugColor::getBlue()
{
   return mRGB[2];
}

void SugColor::getRGB(SugRealType& r, SugRealType& g, SugRealType& b)
{
   r = mRGB[0];
   g = mRGB[1];
   b = mRGB[2];
}

void SugColor::setRGB(SugRealType r, SugRealType g, SugRealType b)
{
   mRGB[0] = r;
   mRGB[1] = g;
   mRGB[2] = b;
}

void SugColor::setColor(const char* colorStr)
{
   short r, g, b;
   unsigned int i;
   unsigned int color;
   
   if (*colorStr == '#')
   {
      colorStr++;
      sscanf(colorStr, "%x", &color);
   }
   else
   {
      char* upperCaseColorStr;

      upperCaseColorStr = new char[strlen(colorStr) + 1];
      strcpy(upperCaseColorStr, colorStr);
      
      for (i=0; i<strlen(upperCaseColorStr); i++)
         upperCaseColorStr[i] = toupper(upperCaseColorStr[i]);
      
      color = gColors[upperCaseColorStr];

      delete upperCaseColorStr;
   }

   r = color >> 16;
   g = (color >> 8) & 0xFF;
   b = color & 0xFF;

   mRGB[0] = r / 255.0;
   mRGB[1] = g / 255.0;
   mRGB[2] = b / 255.0;
}

SugColor::operator SugRealType*()
{
   return mRGB;
}

void SugColor::addColorAlias(string colorName, unsigned int colorValue)
{
   gColors[colorName] = colorValue;
}

SugColor::SugColor()
{
   if (!gColorsInitilized)
   {
      addColorAlias("ALICEBLUE", 0xF0F8FF); 
      addColorAlias("ANTIQUEWHITE", 0xFAEBD7);
      addColorAlias("AQUA", 0x00FFFF);
      addColorAlias("AQUAMARINE", 0x7FFFD4);
      addColorAlias("AZURE", 0xF0FFFF);
      addColorAlias("BEIGE", 0xF5F5DC);
      addColorAlias("BISQUE", 0xFFE4C4);
      addColorAlias("BLACK", 0x000000);
      addColorAlias("BLANCHEDALMOND", 0xFFEBCD);
      addColorAlias("BLUE", 0x0000FF);
      addColorAlias("BLUEVIOLET", 0x8A2BE2);
      addColorAlias("BROWN", 0xA52A2A);
      addColorAlias("BURLYWOOD", 0xDEB887);
      addColorAlias("CADETBLUE", 0x5F9EA0);
      addColorAlias("CHARTREUCE", 0x7FFF00);
      addColorAlias("CHOCOLATE", 0xD2691E);
      addColorAlias("CORAL", 0xFF7F50);
      addColorAlias("CORNFLOWERBLUE", 0x6495ED);
      addColorAlias("CORNSILK", 0xFFF8DC);
      addColorAlias("CRIMSON", 0xDC143C);
      addColorAlias("CYAN", 0x00FFFF);
      addColorAlias("DARKBLUE", 0x00008B);
      addColorAlias("DARKCYAN", 0x008B8B);
      addColorAlias("DARKGOLDENROD", 0xB8860B);
      addColorAlias("DARKGRAY", 0xA9A9A9);
      addColorAlias("DARKGREEN", 0x006400);
      addColorAlias("DARKKHAKI", 0xBDB76B);
      addColorAlias("DARKMAGENTA", 0x8B008B);
      addColorAlias("DARKOLIVEGREEN", 0x556B2F);
      addColorAlias("DARKORANGE", 0xFF8C00);
      addColorAlias("DARKORCHID", 0x9932CC);
      addColorAlias("DARKRED", 0x8B0000);
      addColorAlias("DARKSALMON", 0xE9967A);
      addColorAlias("DARKSEAGREEN", 0x8FBC8F);
      addColorAlias("DARKSLATEBLUE", 0x483D8B);
      addColorAlias("DARKSLATEGRAY", 0x2F4F4F);
      addColorAlias("DARKTURQUOISE", 0x00CED1);
      addColorAlias("DARKVIOLET", 0x9400D3);
      addColorAlias("DEEPPINK", 0xFF1493);
      addColorAlias("DEEPSKYBLUE", 0x00BFFF);
      addColorAlias("DIMGRAY", 0x696969);
      addColorAlias("DODGERBLUE", 0x1E90FF);
      addColorAlias("FELDSPAR", 0x1E90FF);
      addColorAlias("FIREBRICK", 0xB22222);
      addColorAlias("FLORALWHITE", 0xFFFAF0);
      addColorAlias("FUCHSIA", 0xFF00FF);
      addColorAlias("GAINSBORO", 0xDCDCDC);
      addColorAlias("GHOSTWHITE", 0xF8F8FF);
      addColorAlias("GOLD", 0xFFD700);
      addColorAlias("GOLDENROD", 0xDAA520);
      addColorAlias("GRAY", 0x808080);
      addColorAlias("GREEN", 0x008000);
      addColorAlias("GREENYELLOW", 0xADFF2F);
      addColorAlias("HONEYDEW", 0xF0FFF0);
      addColorAlias("HOTPINK", 0xFF69B4);
      addColorAlias("INDIANRED", 0xCD5C5C);
      addColorAlias("INDIGO", 0x480082);
      addColorAlias("IVORY", 0xFFFFF0);
      addColorAlias("KHAKI", 0xF0E68C);
      addColorAlias("LAVENDER", 0xE6E6FA);
      addColorAlias("LAVENDERBLUSH", 0xFFF0F5);
      addColorAlias("LAWNGREEN", 0x7CFC00);
      addColorAlias("LEMONCHIFFON", 0xFFFACD);
      addColorAlias("LIGHTBLUE", 0xADD8E6);
      addColorAlias("LIGHTCORAL", 0xF08080);
      addColorAlias("LIGHTCYAN", 0xE0FFFF);
      addColorAlias("LIGHTGOLDENRODYELLOW", 0xFAFAD2);
      addColorAlias("LIGHTGREY", 0xD3D3D3);
      addColorAlias("LIGHTGREEN", 0x90EE90);
      addColorAlias("LIGHTPINK", 0xFFB6C1);
      addColorAlias("LIGHTSALMON", 0xFFA07A);
      addColorAlias("LIGHTSEAGREEN", 0x20B2AA);
      addColorAlias("LIGHTSKYBLUE", 0x87CEFA);
      addColorAlias("LIGHTSLATEBLUE", 0x8470FF);
      addColorAlias("LIGHTSLATEGRAY", 0x778899);
      addColorAlias("LIGHTSTEELBLUE", 0xB0C4DE);
      addColorAlias("LIGHTYELLOW", 0xFFFFE0);
      addColorAlias("LIME", 0x00FF00);
      addColorAlias("LIMEGREEN", 0x32CD32);
      addColorAlias("LINEN", 0xFAF0E6);
      addColorAlias("MAGENTA", 0xFF00FF);
      addColorAlias("MAROON", 0x800000);
      addColorAlias("MEDIUMAQUAMARINE", 0x66CDAA);
      addColorAlias("MEDIUMBLUE", 0x0000CD);
      addColorAlias("MEDIUMORCHID", 0xBA55D3);
      addColorAlias("MEDIUMPURPLE", 0x9370D8);
      addColorAlias("MEDIUMSEAGREEN", 0x3CB371);
      addColorAlias("MEDIUMSLATEBLUE", 0x7B68EE);
      addColorAlias("MEDIUMSPRINGGREEN", 0x00FA9A);
      addColorAlias("MEDIUMTURQUOISE", 0x48D1CC);
      addColorAlias("MEDIUMVIOLETRED", 0xC71585);
      addColorAlias("MIDNIGHTBLUE", 0x191970);
      addColorAlias("MINTCREAM", 0xF5FFFA);
      addColorAlias("MISTYROSE", 0xFFE4E1);
      addColorAlias("MOCCASIN", 0xFFE4B5);
      addColorAlias("NAVAJOWHITE", 0xFFDEAD);
      addColorAlias("NAVY", 0x000080);
      addColorAlias("OLDLACE", 0xFDF5E6);
      addColorAlias("OLIVE", 0x808000);
      addColorAlias("OLIVEDRAB", 0x6B8E23);
      addColorAlias("ORANGE", 0xFFA500);
      addColorAlias("ORANGERED", 0xFF4500);
      addColorAlias("ORCHID", 0xDA70D6);
      addColorAlias("PALEGOLDENROD", 0xEEE8AA);
      addColorAlias("PALEGREEN", 0x98FB98);
      addColorAlias("PALETURQUOISE", 0xAFEEEE);
      addColorAlias("PALEVIOLETRED", 0xD87093);
      addColorAlias("PAPAYAWHIP", 0xFFEFD5);
      addColorAlias("PEACHPUFF", 0xFFDAB9);
      addColorAlias("PERU", 0xCD853F);
      addColorAlias("PINK", 0xFFC0CB);
      addColorAlias("PLUM", 0xDDA0DD);
      addColorAlias("POWDERBLUE", 0xB0E0E6);
      addColorAlias("PURPLE", 0x800080);
      addColorAlias("RED", 0xFF0000);
      addColorAlias("ROSYBROWN", 0xBC8F8F);
      addColorAlias("ROYALBLUE", 0x4169E1);
      addColorAlias("SADDLEBROWN", 0x8B4513);
      addColorAlias("SALMON", 0xFA8072);
      addColorAlias("SANDYBROWN", 0xF4A460);
      addColorAlias("SEAGREEN", 0x2E8B57);
      addColorAlias("SIENNA", 0xA0522D);
      addColorAlias("SILVER", 0xC0C0C0);
      addColorAlias("SKYBLUE", 0x87CEEB);
      addColorAlias("SLATEBLUE", 0x6A5ACD);
      addColorAlias("SLATEGRAY", 0x708090);
      addColorAlias("SNOW", 0xFFFAFA);
      addColorAlias("SPRINGGREEN", 0x00FF7F);
      addColorAlias("STEELBLUE", 0x4682B4);
      addColorAlias("TAN", 0xD2B48C);
      addColorAlias("TEAL", 0x008080);
      addColorAlias("THISTLE", 0xD8BFD8);
      addColorAlias("TOMATO", 0xFF6347);
      addColorAlias("TURQUOISE", 0x40E0D0);
      addColorAlias("VIOLET", 0xEE82EE);
      addColorAlias("VIOLETRED", 0xD02090);
      addColorAlias("WHEAT", 0xF5D3B3);
      addColorAlias("WHITE", 0xFFFFFF);
      addColorAlias("WHITESMOKE", 0xF5F5F5);
      addColorAlias("YELLOW", 0xFFFF00);
      addColorAlias("YELLOWGREEN", 0x9ACD32);

      /*
      gColors["ALICEBLUE"] = 0xF0F8FF; 
      gColors["ANTIQUEWHITE"] = 0xFAEBD7;
      gColors["AQUA"] = 0x00FFFF;
      gColors["AQUAMARINE"] = 0x7FFFD4;
      gColors["AZURE"] = 0xF0FFFF;
      gColors["BEIGE"] = 0xF5F5DC;
      gColors["BISQUE"] = 0xFFE4C4;
      gColors["BLACK"] = 0x000000;
      gColors["BLANCHEDALMOND"] = 0xFFEBCD;
      gColors["BLUE"] = 0x0000FF;
      gColors["BLUEVIOLET"] = 0x8A2BE2;
      gColors["BROWN"] = 0xA52A2A;
      gColors["BURLYWOOD"] = 0xDEB887;
      gColors["CADETBLUE"] = 0x5F9EA0;
      gColors["CHARTREUCE"] = 0x7FFF00;
      gColors["CHOCOLATE"] = 0xD2691E;
      gColors["CORAL"] = 0xFF7F50;
      gColors["CORNFLOWERBLUE"] = 0x6495ED;
      gColors["CORNSILK"] = 0xFFF8DC;
      gColors["CRIMSON"] = 0xDC143C;
      gColors["CYAN"] = 0x00FFFF;
      gColors["DARKBLUE"] = 0x00008B;
      gColors["DARKCYAN"] = 0x008B8B;
      gColors["DARKGOLDENROD"] = 0xB8860B;
      gColors["DARKGRAY"] = 0xA9A9A9;
      gColors["DARKGREEN"] = 0x006400;
      gColors["DARKKHAKI"] = 0xBDB76B;
      gColors["DARKMAGENTA"] = 0x8B008B;
      gColors["DARKOLIVEGREEN"] = 0x556B2F;
      gColors["DARKORANGE"] = 0xFF8C00;
      gColors["DARKORCHID"] = 0x9932CC;
      gColors["DARKRED"] = 0x8B0000;
      gColors["DARKSALMON"] = 0xE9967A;
      gColors["DARKSEAGREEN"] = 0x8FBC8F;
      gColors["DARKSLATEBLUE"] = 0x483D8B;
      gColors["DARKSLATEGRAY"] = 0x2F4F4F;
      gColors["DARKTURQUOISE"] = 0x00CED1;
      gColors["DARKVIOLET"] = 0x9400D3;
      gColors["DEEPPINK"] = 0xFF1493;
      gColors["DEEPSKYBLUE"] = 0x00BFFF;
      gColors["DIMGRAY"] = 0x696969;
      gColors["DODGERBLUE"] = 0x1E90FF;
      gColors["FELDSPAR"] = 0x1E90FF;
      gColors["FIREBRICK"] = 0xB22222;
      gColors["FLORALWHITE"] = 0xFFFAF0;
      gColors["FUCHSIA"] = 0xFF00FF;
      gColors["GAINSBORO"] = 0xDCDCDC;
      gColors["GHOSTWHITE"] = 0xF8F8FF;
      gColors["GOLD"] = 0xFFD700;
      gColors["GOLDENROD"] = 0xDAA520;
      gColors["GRAY"] = 0x808080;
      gColors["GREEN"] = 0x008000;
      gColors["GREENYELLOW"] = 0xADFF2F;
      gColors["HONEYDEW"] = 0xF0FFF0;
      gColors["HOTPINK"] = 0xFF69B4;
      gColors["INDIANRED"] = 0xCD5C5C;
      gColors["INDIGO"] = 0x480082;
      gColors["IVORY"] = 0xFFFFF0;
      gColors["KHAKI"] = 0xF0E68C;
      gColors["LAVENDER"] = 0xE6E6FA;
      gColors["LAVENDERBLUSH"] = 0xFFF0F5;
      gColors["LAWNGREEN"] = 0x7CFC00;
      gColors["LEMONCHIFFON"] = 0xFFFACD;
      gColors["LIGHTBLUE"] = 0xADD8E6;
      gColors["LIGHTCORAL"] = 0xF08080;
      gColors["LIGHTCYAN"] = 0xE0FFFF;
      gColors["LIGHTGOLDENRODYELLOW"] = 0xFAFAD2;
      gColors["LIGHTGREY"] = 0xD3D3D3;
      gColors["LIGHTGREEN"] = 0x90EE90;
      gColors["LIGHTPINK"] = 0xFFB6C1;
      gColors["LIGHTSALMON"] = 0xFFA07A;
      gColors["LIGHTSEAGREEN"] = 0x20B2AA;
      gColors["LIGHTSKYBLUE"] = 0x87CEFA;
      gColors["LIGHTSLATEBLUE"] = 0x8470FF;
      gColors["LIGHTSLATEGRAY"] = 0x778899;
      gColors["LIGHTSTEELBLUE"] = 0xB0C4DE;
      gColors["LIGHTYELLOW"] = 0xFFFFE0;
      gColors["LIME"] = 0x00FF00;
      gColors["LIMEGREEN"] = 0x32CD32;
      gColors["LINEN"] = 0xFAF0E6;
      gColors["MAGENTA"] = 0xFF00FF;
      gColors["MAROON"] = 0x800000;
      gColors["MEDIUMAQUAMARINE"] = 0x66CDAA;
      gColors["MEDIUMBLUE"] = 0x0000CD;
      gColors["MEDIUMORCHID"] = 0xBA55D3;
      gColors["MEDIUMPURPLE"] = 0x9370D8;
      gColors["MEDIUMSEAGREEN"] = 0x3CB371;
      gColors["MEDIUMSLATEBLUE"] = 0x7B68EE;
      gColors["MEDIUMSPRINGGREEN"] = 0x00FA9A;
      gColors["MEDIUMTURQUOISE"] = 0x48D1CC;
      gColors["MEDIUMVIOLETRED"] = 0xC71585;
      gColors["MIDNIGHTBLUE"] = 0x191970;
      gColors["MINTCREAM"] = 0xF5FFFA;
      gColors["MISTYROSE"] = 0xFFE4E1;
      gColors["MOCCASIN"] = 0xFFE4B5;
      gColors["NAVAJOWHITE"] = 0xFFDEAD;
      gColors["NAVY"] = 0x000080;
      gColors["OLDLACE"] = 0xFDF5E6;
      gColors["OLIVE"] = 0x808000;
      gColors["OLIVEDRAB"] = 0x6B8E23;
      gColors["ORANGE"] = 0xFFA500;
      gColors["ORANGERED"] = 0xFF4500;
      gColors["ORCHID"] = 0xDA70D6;
      gColors["PALEGOLDENROD"] = 0xEEE8AA;
      gColors["PALEGREEN"] = 0x98FB98;
      gColors["PALETURQUOISE"] = 0xAFEEEE;
      gColors["PALEVIOLETRED"] = 0xD87093;
      gColors["PAPAYAWHIP"] = 0xFFEFD5;
      gColors["PEACHPUFF"] = 0xFFDAB9;
      gColors["PERU"] = 0xCD853F;
      gColors["PINK"] = 0xFFC0CB;
      gColors["PLUM"] = 0xDDA0DD;
      gColors["POWDERBLUE"] = 0xB0E0E6;
      gColors["PURPLE"] = 0x800080;
      gColors["RED"] = 0xFF0000;
      gColors["ROSYBROWN"] = 0xBC8F8F;
      gColors["ROYALBLUE"] = 0x4169E1;
      gColors["SADDLEBROWN"] = 0x8B4513;
      gColors["SALMON"] = 0xFA8072;
      gColors["SANDYBROWN"] = 0xF4A460;
      gColors["SEAGREEN"] = 0x2E8B57;
      gColors["SIENNA"] = 0xA0522D;
      gColors["SILVER"] = 0xC0C0C0;
      gColors["SKYBLUE"] = 0x87CEEB;
      gColors["SLATEBLUE"] = 0x6A5ACD;
      gColors["SLATEGRAY"] = 0x708090;
      gColors["SNOW"] = 0xFFFAFA;
      gColors["SPRINGGREEN"] = 0x00FF7F;
      gColors["STEELBLUE"] = 0x4682B4;
      gColors["TAN"] = 0xD2B48C;
      gColors["TEAL"] = 0x008080;
      gColors["THISTLE"] = 0xD8BFD8;
      gColors["TOMATO"] = 0xFF6347;
      gColors["TURQUOISE"] = 0x40E0D0;
      gColors["VIOLET"] = 0xEE82EE;
      gColors["VIOLETRED"] = 0xD02090;
      gColors["WHEAT"] = 0xF5D3B3;
      gColors["WHITE"] = 0xFFFFFF;
      gColors["WHITESMOKE"] = 0xF5F5F5;
      gColors["YELLOW"] = 0xFFFF00;
      gColors["YELLOWGREEN"] = 0x9ACD32;
      */
   }
   gColorsInitilized = true;
   
   mRGB[0] = 0.0;
   mRGB[1] = 0.0;
   mRGB[2] = 0.0;
}

SugColor::~SugColor()
{
}
